// pages/login/login.js
const ui = require('../../utils/ui')

const APP = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userPhone: '',
    isPasswordLogin: false,
    radio: false,
    agreement: false,
    codeButton: '发送验证码',
    codeButtonEnable: false,
    codeSecond: 60,
    shareCode: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      shareCode: options.shareCode
    })
    console.log('=========code===:::'+this.data.shareCode);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //切换到密码登录
  changToPassword() {
    this.setData({
      isPasswordLogin: true
    })
  },
  //切换到验证码登录
  changToCode() {
    this.setData({
      isPasswordLogin: false
    })
  },
  //点击是否同意协议
  agreePress() {
    this.setData({
      agreement: true,
    });
  },
  disagreePress() {
    this.setData({
      agreement: false,
    });
  },
  //点击隐私协议
  yinsiAgreementPress() {
    wx.navigateTo({
      url: '/pages/login/showWeb?url=https://www.shangjinxia.ltd/app_task/privacyProtocol/index.html',
    })
  },
  //点击用户协议
  yonghuAgreementPress() {
    wx.navigateTo({
      url: '/pages/login/showWeb?url=https://www.shangjinxia.ltd/app_task/serverProtocol/index.html',
    })
  },
  //发送验证码
  sendCode() {
    var that = this;
    if (this.data.userPhone.length == 0) {
      ui.showToast('请输入手机号');
      return;
    }
    wx.request({
      
      url: APP.globalData.hostIp + '/user/' + this.data.userPhone + '/sms',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {},
      method: 'GET',
      success: function (res) {
        if (res.statusCode == 200) {
          //此处省略发送短信验证码功能
          that.setData({
            codeButtonEnable: true
          })
          that.timer()
        } else {
          ui.showToast(res.data.error_message);
        }
      },
      fail: function (res) {
        ui.showToast(res.data.error_message);
      }
    })

  },
  //倒计时
  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            codeSecond: this.data.codeSecond - 1
          })
          if (this.data.codeSecond <= 0) {
            this.setData({
              codeSecond: 60,
              codeButtonEnable: false,
              codeButton: '重新发送'
            })
            resolve(setTimer)
          } else {
            this.setData({
              codeButton: this.data.codeSecond + 's'
            })
          }
        }, 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },
  //修改密码
  actionPassword() {
    wx.navigateTo({
      url: '/pages/setting/editPassword',
    })
  },
  //登录
  login_submit(event) {
    if (this.data.isPasswordLogin) {
      if (event.detail.value.amount.length == 0) {
        ui.showToast('请输入账号');
        return;
      }
      if (event.detail.value.password.length == 0) {
        ui.showToast('请输入密码');
        return;
      }
      if (!this.data.agreement) {
        wx.showModal({
          title: '提示',
          content: '需要勾选同意《隐私协议》和《用户协议》才可以进行登录操作',
          confirmText: '同意',
          complete: (res) => {
            if (res.confirm) {
              this.setData({
                agreement: true,
              });
            }
          }
        });
        return;
      }
      wx.request({
        url: APP.globalData.hostIp + '/user/login-with-mobile-password',
        data: {
          mobile: event.detail.value.amount,
          password: event.detail.value.password,
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: function (res) {
          if (res.statusCode == 200) {
            wx.setStorageSync('USERDATA', res.data);
            wx.showToast({
              title: '登录成功',
              icon: 'success',
              duration: 2000,
              mask: true,
              success: function() {
                setTimeout(function() {
                  wx.navigateBack({
                    delta: 0,
                  });
                }, 1500)
              }
            });
          } else {
            ui.showToast(res.data.error_message);
          }
        },
        fail: function (res) {
          ui.showToast(res.data.error_message);
        }
      })
    } else {
      if (event.detail.value.phone.length == 0) {
        ui.showToast('请输入手机号');
        return;
      }
      if (event.detail.value.code.length == 0) {
        ui.showToast('请输入验证码');
        return;
      }
      if (!this.data.agreement) {
        wx.showModal({
          title: '提示',
          content: '需要勾选同意《隐私协议》和《用户协议》才可以进行登录操作',
          confirmText: '同意',
          complete: (res) => {
            if (res.confirm) {
              this.setData({
                agreement: true,
              });
            }
          }
        });
        return;
      }
      wx.request({
        url: APP.globalData.hostIp + '/user',
        data: {
          mobile: event.detail.value.phone,
          verifyCode: event.detail.value.code,
          shareCode: this.data.shareCode
        },
        method: 'POST',
        success: function (res) {
          if (res.statusCode == 200) {
            wx.setStorageSync('USERDATA', res.data);
            wx.showToast({
              title: '登录成功',
              icon: 'success',
              duration: 2000,
              mask: true,
              success: function() {
                setTimeout(function() {
                  wx.navigateBack({
                    delta: 0,
                  });
                }, 1500)
              }
            });
          } else {
            ui.showToast(res.data.error_message);
          }
        },
        fail: function (res) {
          ui.showToast(res.data.error_message);
        }
      })
    }

  }
})